﻿using System.Linq;
using CRM.Database.Model;

namespace CRM.Syncroniser
{
    public class ScheduledCoursesSyncroniser:ISyncronise 
    {
        #region Implementation of ISyncronise

        /// <summary>
        /// Запуск синхронизации
        /// </summary>
        /// <returns>Результат запуска</returns>
        public bool Syncronise()
        {
            foreach(var item in CDBCourses)
            {
              

                var col = new Database.Model.TrainingsCoursesCollection();
                int idCourse = col.GetIdByName(item.Name);
                if(idCourse>0)
                {

                    if(CRMCourses.TrainingsSchedulesList.FirstOrDefault(c=>c.CourseName==item.Name & c.PlannedDate==item.Date)==null)
                    {
                          var sc = new Database.Model.TrainingsSchedules();

                    sc.PlannedDate = item.Date;
                    sc.IdTrainingsCource = idCourse;
                    int idtrainingPlace = 0;
                    int idprofineregion = 0;

                    switch(item.LearningLocationName)
                    {
                        case "Москва":
                            idtrainingPlace = 1;
                            idprofineregion = 1;
                            break;
                        case "Ростов-на-Дону":
                            idtrainingPlace = 2;
                            idprofineregion = 3;
                            break;
                        case "Самара":
                            idtrainingPlace = 3;
                            idprofineregion = 4;
                            break;
                        case "Пермь":
                            idtrainingPlace = 9;
                            idprofineregion = 5;
                            break;
                        case "Екатеринбург":
                            idtrainingPlace = 6;
                            idprofineregion = 5;
                            break;
                        case "Челябинск":
                            idtrainingPlace = 10;
                            idprofineregion = 5;
                            break;
                    }
                    if(idtrainingPlace>0) sc.IdTrainingsPlace = idtrainingPlace;

                    var trCol = new TrainingsTrainersCollection();
                    int idTrainer = trCol.GetIdByName(item.LearningTrainer.Name);
                    if(idTrainer>0) sc.IdTrainingsTrainer = idTrainer;

                    sc.IdTrainingCoursesType = 1;
                    sc.IdTrainingCoursesStatus = 5;

                        sc.IdProfineRegion = idprofineregion;

                    CRMCourses.Add(sc, "Max");

                        var colClaims = new TrainingsRequestsCollection();

                        foreach(var req in item.Claims)
                        {

                            var crmClaim = new Database.Model.TrainingsRequests();
                            crmClaim.IdTrainingsCourse = sc.IdTrainingsCource;
                            crmClaim.IdTrainingsSchedule = sc.IdTrainingsSchedule;
                            crmClaim.RequestDate = req.ClaimDate;
                            crmClaim.Company = req.ClientName;
                            crmClaim.ContactName = req.Contactor.Name;
                            crmClaim.ContactPhone = req.Contactor.PhoneNumber;
                            crmClaim.ContactEmail = req.Contactor.Email;
                            crmClaim.Comments = req.Contactor.Comment;
                            crmClaim.IdTrainingsRequestsStatus = 5;

                            int seqNum = req.Client.SequentialNumber;
                            var colCust = new CustomersCollection();
                            var cust = colCust.GetCustomerBySequentalNumber(seqNum);
                            var cont = cust.Contacts.FirstOrDefault(c => c.Name == req.Contactor.Name);
                            if (cont==null)
                            {
                                var newCont = new Contacts
                                                  {
                                                      Email = req.Contactor.Email,
                                                      Birthday = req.Contactor.DateOfBirth,
                                                      Comments = req.Contactor.Comment,
                                                      IdLegalEntity = cust.LegalEntities.First().IdLegalEntity,
                                                      IsActive = true,Phone=req.Contactor.PhoneNumber,Position=req.Contactor.Post,
                                                      Name=req.Contactor.Name
                                                  };
                                var contactCol = new ContactsCollection();
                                contactCol.Add(newCont, "MAx");
                                cont = newCont;
                            }
                            crmClaim.IdCustomer = cust.IdCustomer;
                            crmClaim.IdContact = cont.IdContact;

                            crmClaim.IdLegalEntity = cont.IdLegalEntity;

                            colClaims.Add(crmClaim, "max");

                            var partCol = new TrainingsParticipantsCollection();

                            foreach(var mem in req.Members)
                            {
                                var crmPart = new TrainingsParticipants
                                                  {
                                                      IdTrainingsParticipantsStatus = 2,
                                                      IdTrainingsRequest = crmClaim.IdTrainingsRequest,
                                                      ParticipantName = mem.ContactorName,
                                                      ParticipantPosition = mem.Contactor.Post
                                                  };

                                partCol.Add(crmPart, "max");

                            }

                        }
                    }


                  
                }

            }

            return true;
        }

        #endregion


        /// <summary>
        /// Участники курсов в CRM
        /// </summary>
        public Database.Model.TrainingsParticipantsCollection CRMParticipants
        {
            get { return new TrainingsParticipantsCollection(); }
        }

        /// <summary>
        /// Курсы из CDB
        /// </summary>
        public DbInterface.LearningClientsPlanCourses CDBCourses
        {
            get { return DbInterface.DataLayer.LearningClientsPlanCourses; }
        }

        /// <summary>
        /// Запланированные курсы из CRM
        /// </summary>
        public Database.Model.TrainingsSchedulesCollection CRMCourses
        {
            get { return new TrainingsSchedulesCollection(); }
        }
    }
}
